#include <iostream>
#include <memory>
#include "calServer.hpp"
#include "err.hpp"

void Usage()
{
    std::cout << "Please enter the correct format: " << std::endl
              << "      ./CalServer [port]" << std::endl;
}

Response calculator(const Request &req)
{
    Response resp;
    switch (req._opt)
    {
    case '+':
        resp._ret = req._x + req._y;
        break;
    case '-':
        resp._ret = req._x - req._y;
        break;
    case '*':
        resp._ret = req._x * req._y;
        break;
    case '/':
        if (req._y == 0)
            resp._code = 1;
        else
            resp._ret = req._x / req._y;
        break;
    case '%':
        if (req._y == 0)
            resp._code = 2;
        else
            resp._ret = req._x % req._y;
        break;
    default:
        resp._code = 3;
    }
    return resp;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage();
        exit(USAGE_ERR);
    }

    std::unique_ptr<CalServer> usvr(new CalServer(calculator, atoi(argv[1])));

    usvr->Initial();
    usvr->Start();
}